import { getToken } from '@/utils/storage'
import { Toast } from 'vant'
import Vue from 'vue'
import VueRouter from 'vue-router'
// import layout from '../views/layout.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: () => import('../views/login.vue') },
  { path: '/register', component: () => import('../views/register.vue') },
  { path: '/detail', component: () => import('../views/detail.vue') },
  {
    path: '/',
    component: () => import('../views/layout.vue'),
    redirect: '/article',
    children: [
      { path: '/article', component: () => import('../views/article.vue') },
      { path: '/like', component: () => import('../views/like.vue') },
      { path: '/collect', component: () => import('../views/collect.vue') },
      { path: '/user', component: () => import('../views/user.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      Toast.fail('请登录')
    }
  }
})

export default router
